<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/excite-bike/jquery-ui-1.8.22.custom.css" rel="stylesheet" type="text/css" />
<link href="css/table.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.7.2.min.js" ></script>
<script src="js/jquery.cookie.js"></script>
<script src="js/jquery-ui-1.8.22.custom.min.js"></script>
<script src="js/widgets.js"></script>
<script type="text/javascript">


	function Click_Reset() {
		$.each($("#q_form tbody input"), function(i, val) { //將每個欄位都設為空字串
			val["value"] = "";
		});
		$("#selectedRole li").remove();
		$("#message").html("");
		$("#errorMessage").html("");
		$("#sub_getUserType")[0].value = null;
		$.cookie("currStatus", null);
	}
	


	function cc() {
		$("#errorMessage").html("");	
		$("#message").html("");
		var isNotBlank = 0;
		var str = "";
		var submitId = $.trim($("#submitId")[0].value);
		var submitPWD = $.trim($("#submitPWD")[0].value);
		var submitNickName = $.trim($("#submitNickName")[0].value);
		
		$("#submitUIText")[0].value = $("option:selected").attr("id");
		if ((!submitId=="") && (!submitPWD=="") && (!submitNickName=="")) {
			isNotBlank = 1;
		}
		if (isNotBlank == 0) {
			$("#errorMessage").html("請至少輸入人員ID、密碼與暱稱 !");
		} else {
			var size = $("#selectedRole li").size();
			var tmp = "";
			if (size>0){
				var sr = $("#selectedRole li");
				$.each(sr,function(i){
					var val= $(this).attr("val");
					tmp += val;
					tmp += ",";
				});
				$("#submitRole")[0].value = tmp;
			} else {
				$("#submitRole")[0].value = 0;
			}
			
			  
			
			var x = $("#q_form").serializeArray();
			$.ajax({
				type : "post",
				url : "CreateUser",
				data : x,
				dataType : "text",
				success : function(submitResult) {
					alert(submitResult);
				}, error : function(xhr, ajaxOptions, thrownError) {
					$("#errorMessage").html("錯誤訊息狀態：" + xhr.status + "<p>");
					$("#errorMessage").append(xhr.responseText);
				}
			});	
		}
	}

	$(document).ready(function() {
		
		$("#loginUser")[0].value = $.cookie("userID");
		$("#dialog-Group").css("visibility", "hidden");
		
		var status = $.cookie("currStatus");
		if (status == null || status != "updateUser") {
			$("#submitId").removeAttr("disabled");
			$("#submitPWD").val(null);
			$("#submitLName").val(null);
			$("#submitFName").val(null);
			$("#submitNickName").val(null);
			$("#submitEmail").val(null);
			$("#selectedRole li").remove();
		} else {
			var userID = $.cookie("upadateUserID");
			
			$("#sub_getUserType")[0].value = userID;
			$.post("GetUsers",$("#q_form").serializeArray(),function(queryResult) {
				$.each(queryResult,function(i, res) {
					$("#submitId").val(res.userID);
					$("#submitPWD").val(res.pwd);
					$("#submitLName").val(res.lastName);
					$("#submitFName").val(res.firstName);
					$("#submitNickName").val(res.nickName);
					$("#submitEmail").val(res.email);
					var tpoStr ="";
					$.each(res.roles,function(i, role) {
						$("#selectedRole").append("<li class=\"selectedRole\" roleName=\""+role.roleName+"\" val=\""+role.roleID+"\" id="+role.roleID+">"+role.roleName+"</li>");
						tpoStr += role.roleID +","; 
					});
					
					/** 將選取的人員，加到q_form的欄位 **/
					if (tpoStr.length != 0){
						$("#sub_GroupList")[0].value = tpoStr;
					}
				});
				$("#submitId").attr("disabled","disabled");
			},"JSON");
		}
	});
</script>



<title>使用者資訊</title>
</head>

<body>
	<form id="q_form" method="post" >
		<table>
			<thead>
				<tr>
					<th colspan="2">使用者資訊</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<th>人員ID<font face="標準體" color="#FF0000"><sub>*</sub></font></th>
					<td><input type="text" id="submitId" name="submitId" class="require_from_group" maxlength="15" size="13" /></td>
				</tr>
				<tr>
					<th>密碼<font face="標準體" color="#FF0000"><sub>*</sub></font></th>
					<td><input type="password" id="submitPWD" name="submitPWD" class="require_from_group" size="13" /></td>
				</tr>
				<tr>
					<th>暱稱<font face="標準體" color="#FF0000"><sub>*</sub></font></th>
					<td><input type="text" id="submitNickName" name="submitNickName" class="require_from_group" maxlength="15" size="13" /></td>
				</tr>
				<tr>
					<th>姓氏</th>
					<td><input type="text" id="submitLName" name="submitLName" maxlength="15" size="13" /></td>
				</tr>
				<tr>
					<th>名稱</th>
					<td><input type="text" id="submitFName" name="submitFName" maxlength="15" size="13" /></td>
				</tr>
				<tr>
					<th>Email</th>
					<td><input type="text" id="submitEmail" name="submitEmail" /></td>
				</tr>
				<tr>
					<th>角色</th>
					<td>
						<button type="button" id="clickGroup" onClick="click_SelectRole()">選擇</button>
						<ul id='selectedRole'></ul>
					</td>
				</tr>
				<tr>
					<th class="warning">*為必填</th>
					<td>
						<button type="button" id="clickS" onClick="cc()">送出</button>
						<button type="button" id="clickReset" onClick="Click_Reset()">重設</button>
					</td>
				</tr>
			</tbody>
		</table>
		<input type="hidden" id="submitRole" name="submitRole" value="" />
		<input type="hidden" id="submitUIText" name="submitUIText" />
		<input type="hidden" id="loginUser" name="loginUser" />
		<input type="hidden" id="sub_GroupList" name="groupList" />
		<input type="hidden" id="sub_getUserType" name="getUserType" />
	</form>

	<div id="message"></div>
	<div id="errorMessage" class="alertText"></div>
	
	
	
	
	<div id="dialog-Group" title="勾選相關角色">
		<table>
			<thead>
				<tr>
					<th><input type="checkbox" id="selectedRoleCheckALL" onClick="chkALL('selectedRole')" /></th>
					<th>#</th>
					<th>角色描述</th>
				</tr>
			</thead>
			
			<tbody id="selectRole">
			
			</tbody>
		</table>
	</div>
	
</body>
</html>